//
//  WDStarRateView.swift
//  WDFavourModule
//
//  Created by Alean on 2023/2/24.
//

import UIKit
//import PublicResources
import SnapKit

public class WDStarRateView: UIView {
    private var numberOfStar = 5
    private var flex: Float = 4
    private var size = CGSize(width: 24, height: 24)

    private var starForegroundView: UIView?
    private var starBackgroundView: UIView?

    /// 0~numberOfStar
    private var _currentRate: Float = 0
    public var currentRate: Float {
        set {
            _currentRate = newValue
            showStar()
        }
        get {
            return _currentRate
        }
    }

    public override convenience init(frame: CGRect) {
        self.init(numberOfStart: 5, currentRate: 0.0, flex: 4, size: CGSize(width: 24, height: 24))
    }

    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public init(numberOfStart: Int = 5, currentRate: Float = 0.0, flex: Float = 4, size: CGSize = CGSize(width: 24, height: 24), selImage: UIImage? = nil, norImage: UIImage? = nil) {
        super.init(frame: .zero)
        clipsToBounds = true
        self.numberOfStar = numberOfStart
        self.currentRate = currentRate
        self.flex = flex
        self.size = size

//        PublicImage(imageName, PublicBundle("WDFavourModule"))
        let nor = norImage ?? UIImage.ur_pureImage(color: UIColor.lightGray)//icon_star_dark
        let sel = selImage ?? UIImage.ur_pureImage(color: UIColor.systemRed)//icon_star_light
        starBackgroundView = createStarView(image: norImage)
        addSubview(starBackgroundView!)
        starForegroundView = createStarView(image: sel, needEnd: false)
        addSubview(starForegroundView!)
        starForegroundView!.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        starBackgroundView!.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }

        showStar()
    }

    private func createStarView(image: UIImage?, needEnd: Bool = true) -> UIView {
        let view = UIView()
        view.clipsToBounds = true

        var currentView: UIView? = nil
        for index in  0..<numberOfStar {
//            let imageView = UIImageView(imageName: imageName)
            let imageView = UIImageView(image: image)
            imageView.tag = index
            view.addSubview(imageView)

            imageView.snp.makeConstraints { make in
                make.top.bottom.equalToSuperview()
                make.size.equalTo(size)
                if let currentView = currentView {
                    make.left.equalTo(currentView.snp.right).offset(flex)
                } else {
                    make.left.equalToSuperview()
                }
                if index == numberOfStar - 1 && needEnd {
                    make.right.equalToSuperview()
                }
            }
            currentView = imageView
        }
        return view
    }

    private func showStar() {
        if let starForegroundView = starForegroundView {
            let width = currentRate * Float(size.width) + (currentRate > 0 ? (ceil(currentRate) - 1) : 0) * flex
            UIView.animate(withDuration: 0.0) {
                starForegroundView.snp.remakeConstraints { make in
                    make.width.equalTo(width)
                }
            }
        }
    }
}
